class Solution {
public:
    int candy(vector<int>& ratings) {
      int sum = 0;
      vector<int> nums(ratings.size(),1);
      for(int i = 1;i < nums.size();i++){
        if(ratings[i] > ratings[i-1]){
          nums[i] = nums[i-1] + 1;
        }
      }
      for(int i = nums.size() - 2;i >= 0;i--){
        if(ratings[i] > ratings[i+1] && nums[i] <= nums[i+1]){
          nums[i] = nums[i+1] + 1;
        }
      }
      for(int i = 0;i < nums.size();i++){
        sum += nums[i];
      }
      return sum;
    }
};
